<!--
  Created by IntelliJ IDEA.
  User: Dai
  Date: 2018/4/11
  Time: 10:51
  Remark: 工程配置 -  对象管理 -  项目管理 - 编辑设备树 - 批量增加变流器和电池包
  To change this template use File | Settings | File Templates.
-->
<!--标题栏-->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">批量生成电池包和变流器</h4>
</div>
<!--表格栏-->
<div class="modal-body">
    <div style="width: 100%;height: 200px;">
        <div style="margin-top: 10px; height: 40px">
            <div class="col-sm-7">
                <label>变流器命名：</label>
                <input type="text" style="width: 40px;" disabled>
                <span>流水号</span>
                <input type="text" value="#变流器(PCS)" style="width: 120px" id="pcs_name">
            </div>
            <div class="col-sm-5">
                <label>流水号范围：</label>
                <input type="text" value="1" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();"
                       style="width: 40px" id="pcs_low">
                <span>~</span>
                <input type="text" value="3" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();"
                       style="width: 40px" id="pcs_high">
            </div>
        </div>
        <div style="margin-top: 10px; height: 40px">
            <div class="col-sm-7">
                <label>电池包命名：</label>
                <input type="text" style="width: 40px;" disabled>
                <span>流水号</span>
                <input type="text" value="#电池包(BMS)" style="width: 120px" id="bms_name">
            </div>
            <div class="col-sm-5">
                <label>流水号范围：</label>
                <input type="text" value="1" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();"
                       style="width: 40px" id="bms_low">
                <span>~</span>
                <input type="text" value="3" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();"
                       style="width: 40px" id="bms_high">
            </div>
        </div>
    </div>
</div>
<!--按钮栏-->
<div class="modal-footer">
    <button type="button" class="btn btn-default" onclick="check_info(this)">确定</button>
</div>
<!--该页面的js 未完成部分见TODO -->
<script>
    /**
     * 检查填写的信息是否完整
     */
    function check_info(button) {
        var pcs_name = $("#pcs_name").val().trim();
        var pcs_low = $("#pcs_low").val().trim();
        var pcs_high = $("#pcs_high").val().trim();
        var bms_name = $("#bms_name").val().trim();
        var bms_low = $("#bms_low").val().trim();
        var bms_high = $("#bms_high").val().trim();
        if (pcs_name == ""){
            alert("变流器命名不能为空");
            return false;
        } else if (bms_name == ""){
            alert("电池包命名不能为空")
            return false;
        } else if (pcs_low > pcs_high || bms_low > bms_high) {
            alert("流水号填写错误");
            return false;
        }
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        // 新建变流包
        for (var i = pcs_low; i <= pcs_high; i++){
            zTree.addNodes(null, {
                id: (100 + newCount),
                pId: 0,
                name: i + pcs_name,
                type: 2
            });
        }
        // 新建电池包
        for (var i = bms_low; i <= bms_high; i++){
            zTree.addNodes(null, {
                id: (100 + newCount),
                pId: 0,
                name: i + bms_name,
                type: 3
            });
        }
        /**
         * TODO 批量新建完成就要发送请求保存节点
         * 保存节点请求应返回树，并且重新加载树结构以保证treeNode.id与数据库中的id一致
         */
        // 关闭弹框
        $(button).parent().siblings(":first").find("button").click();
    }
</script>